
import 'package:flutter/widgets.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:hxdj/common/components/my_alert.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'package:hxdj/common/my_pay_manager.dart';
import 'package:hxdj/common/routers/navigator.dart';
import 'package:hxdj/common/utils/my_event_bus_util.dart';
import 'package:hxdj/home/home_viewmodel.dart';
import 'package:hxdj/home/order/order_detail/order_detail_page.dart';
import 'package:hxdj/message/message_page/message_viewmodel.dart';
import 'package:hxdj/message/rongcloud/rc_chat/rc_chat_page.dart';
import 'package:hxdj/mine/pay/select_pay.dart';
import 'package:provider/provider.dart';

Map<String, String> titleMap = {
  "1": "取消订单",
  "2": "立即付款",
  "3": "联系商家",
  "4": "确认完成",
  "5": "立即接单",
  "6": "联系客户",
  "7": "删除订单",
  "8": "再次购买",
  "9": "确认码",
  "10": "我已出发",
  "11": "我已到达",
  "12": "开始服务",
  "13": "完成服务",
  "14": "重要提示",
};

class OrderManager {
//1 全部 2 待支付 3 待消费 4 已完成 5 已取消
  //订单全部
  static String orderAllList = "1";

  //代消费
  static String orderConsumeList = "3";

  //已完成
  static String orderFinishList = "4";

  //已取消
  static String orderCancelList = "5";

  // 包含订单相关的逻辑，如创建订单、查询订单等

  static clickFuntion(String e, BuildContext context, String orderId, String memberId,
      {Function(bool isSuccess)? onSure, String price = "", Map<String, String>? orderPayParmams}) {
    switch (e) {
      case "1":
        cancelOrder(context, orderId, memberId);
        break;
      case "2":
        payOrder(context, orderId, price);
        break;
      case "3":
        break;
      case "4":
        break;
      case "5":
        break;
      case "6":
        break;
      case "7":
        break;
      case "8":
        break;
      case "9":
        break;
      case "10":
      case "11":
        break;
      case "12":
        break;
      case "13":
        break;
      case "14":
        break;
      default:
    }
  }

  //取消订单
  static void cancelOrder(BuildContext context, String orderId, String memberId, {Function(bool isSuccess)? onSure}) {
    // 取消订单的逻辑
    MyAlertManager.showAlert(AlertType.iosMode, context, "您确定要取消订单吗？", "", onSure: () async {
      // 取消订单的操作
      Map<String, dynamic> resultMap = await DioHttpManager().request(MyApi.anmoV2OrderCancel,
          method: DioHttpManager.post, data: {"order_id": orderId, "member_id": memberId});
      if (resultMap["code"] == 0) {
        //取消订单成功后的操作 刷新首页, 以及当前页面
        EasyLoading.showToast("取消成功");

        //刷新首页的订单数据
        HomeViewModel homeViewModel = Provider.of<HomeViewModel>(context, listen: false);
        homeViewModel.reloadOrderData();
        //返回到上一页
        if (Navigator.canPop(context)) {
          Navigator.pop(context);
        }
        // 还有当前页面, 订单全部展示的页面,
        try {
          //刷新全部订单 ,待销费的
          MyEventBusUtil.sentEvent(MyEventBusHelp(MyEventBusTag.onMessageReceived, result: {"order_status": "1"}));
        } catch (e) {}

        if (onSure != null) {
          onSure(true);
        }
      } else {
        EasyLoading.showToast(resultMap['msg']);
        if (onSure != null) {
          onSure(false);
        }
      }
    }, close: "再想想", sure: "确定");
  }

  // 查询订单
  static void queryOrder(String orderId) {
    // 查询订单的逻辑
  }

  //支付订单
  static void payOrder(BuildContext context, String orderId, String price) {
    // 支付订单的逻辑
    // //选择了那个支付方式, 传一个闭包过去
    Future<void> onSelectCallback(String value) async {
      if (value == "ic_wechat_pay") {
        requestPayInfo(context, orderId, "wechaapp");
      } else if (value == "ic_ali_pay") {
        requestPayInfo(context, orderId, "aliapppay");
      } else {
        //其他支付方式
        debugPrint("object $value");
      }
    }

    //查看 当前支付的支持那种方式
    Map<String, dynamic> paramsObjectMap = {"price": price, "onSelectCallback": onSelectCallback};
    MyAlertManager.sheetAlert(AlertType.customWidget, context, "", "", child: SelectPayPage(object: paramsObjectMap));
  }

//请求相关的支付参数,创建订单
  static requestPayInfo(BuildContext context, String orderId, String payType) async {
    // 去请求服务器数据. 下订单
    Map<String, dynamic> paramsMap = {
      "channel_pay": payType,
      "order_id": orderId,
      "type": 20,
    };

    //立即支付的逻辑
    Map<String, dynamic> resultMap =
        await DioHttpManager().request(MyApi.payInfo, method: DioHttpManager.post, data: paramsMap);


    if (resultMap['code'] == 0) {
      //查看 当前支付的支持那种方式
      Map<String, dynamic> resultPayManagerMap = resultMap["data"];
      String aliPayUrl = resultPayManagerMap['pay_url'] ?? "";
      String orderId = resultPayManagerMap['order_id'].toString();

      Map<String, dynamic> weixinUrlMap = resultPayManagerMap['weixin_url'];

      //去支付
      switch (payType) {
        case "aliapppay":
          {
            goAliPay(aliPayUrl, orderId, context);
          }

          break;
        case "wechaapp":
          {
            MyPayManager().goWechatPay(weixinUrlMap, (p0) {});
          }

          break;
        default:
      }

      //刷新首页的订单数据
      HomeViewModel homeViewModel = Provider.of<HomeViewModel>(context, listen: false);
      homeViewModel.reloadOrderData();
    } else {
      EasyLoading.showToast(resultMap["msg"]);
    }
  }

  static goAliPay(String aliPayUrl, String orderId, BuildContext context) {
    MyPayManager().goAliPay(aliPayUrl).then((value) {
      debugPrint("value: $value");
      switch (value["payReuslt"]) {
        case "success":
          MyAlertManager.sheetAlert(AlertType.sheetPayStatue, context, "支付完成", "ic_pay_success",
              object: {"buttonTitle": "查看订单"}, onSure: () {
            Navigator.of(context).popUntil((route) => route.isFirst);
            NavigatorUtils.pushParams(context, '${OrderDetailPage.routeName}?orderId=$orderId');
          });
          break;
        default:
          MyAlertManager.sheetAlert(AlertType.sheetPayStatue, context, "支付失败", "ic_pay_fail",
              object: {"buttonTitle": "重新支付"}, onClose: () {
            Navigator.of(context).popUntil((route) => route.isFirst);
          }, onSure: () {
            goAliPay(aliPayUrl, orderId, context);
          });
          break;
      }
    });
  }

  // 再来一单
  static void placeAnotherOrder(String orderId) {
    // 再来一单的逻辑
  }

  //重新下单
  void reorder(BuildContext context, String orderId) {
    // 重新下单的逻辑
  }

  //删除订单
  static void deleteOrder(BuildContext context, String orderId) {
    // 删除订单的逻辑
  }

  //联系技师
  static void contactTechnician(BuildContext context, String imUserId) {
    // 联系技师的逻辑
    NavigatorUtils.pushParams(context, "${RCChatPage.routeName}?targetId=$imUserId").then((value) {
      MessageViewModel viewModel = Provider.of<MessageViewModel>(context, listen: false);
      viewModel.requetsData();
    });
  }
  // 其他订单相关的逻辑...
}
